package com.bristua.mall.order.biz;

import com.bristua.mall.common.biz.QueryDictionaryBiz;
import com.bristua.mall.common.constant.Constant;
import com.bristua.mall.common.model.TDictionary;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.order.dao.TOrderMapper;
import com.bristua.mall.order.model.TOrder;
import com.bristua.mall.order.model.condition.TOrderExample;
import com.bristua.mall.user.dao.TUserAccountForUpdateMapper;
import com.bristua.mall.user.dao.TUserAccountMapper;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.dao.TUserReturnIntegralFlowMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAccount;
import com.bristua.mall.user.model.TUserReturnIntegralFlow;
import com.bristua.mall.user.model.condition.TUserAccountExample;
import com.bristua.mall.user.model.condition.TUserReturnIntegralFlowExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/4/26.
 */
@Service
@Transactional
public class ConfirmReceiptBiz {

    @Resource
    private TOrderMapper tOrderMapper;

    @Autowired
    private CacheUtil cacheUtil;

    @Resource
    private TUserMapper tUserMapper;

    @Resource
    private TUserAccountMapper tUserAccountMapper;

    @Resource
    private TUserReturnIntegralFlowMapper tUserReturnIntegralFlowMapper;

    @Autowired
    private QueryDictionaryBiz queryDictionaryBiz;

    @Autowired
    private TUserAccountForUpdateMapper tUserAccountForUpdateMapper;


    public void confirmReceipt(String orderId){
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(orderId);
        if(tOrder!=null && tOrder.getPayStaus().equals("2") && tOrder.getSendStatus().equals("2") && tOrder.getConfirmStatus().equals("1")){
            tOrder.setConfirmStatus("2");
            tOrder.setEvaluateStatus("0");
            String confirmTime = DateUtil.getDateTime3();
            tOrder.setConfirmTime(confirmTime);
            tOrderMapper.updateByPrimaryKeySelective(tOrder);
            TUser userSession = (TUser)cacheUtil.getUserSession();
            String parentId = userSession.getParentId();
            if(!StringUtils.isEmpty(parentId)){//给上级返
                TUser tUser = tUserMapper.selectByPrimaryKey(parentId);
                if(tUser!=null){
                    String userLevel = tUser.getUserLevel();
                    if("0".equals(userLevel)){//普通用户
                        TOrderExample tOrderExample = new TOrderExample();
                        tOrderExample.setOrderByClause("create_time asc");
                        tOrderExample.createCriteria().andUserIdEqualTo(tUser.getUserId()).andPayStausEqualTo("2").andSendStatusEqualTo("2").andConfirmStatusEqualTo("2");
                        List<TOrder> tOrders = tOrderMapper.selectByExample(tOrderExample);
                        if(tOrders!=null && tOrders.size()>0){
                            TOrder tOrder1 = tOrders.get(0);//取第一笔订单
                            if(Long.valueOf(tOrder.getOrderPrice())>Long.valueOf(tOrder1.getOrderPrice())){//大于上级的第一笔订单金额,可以进行返现
                                TUserReturnIntegralFlowExample tUserReturnIntegralFlowExample = new TUserReturnIntegralFlowExample();
                                tUserReturnIntegralFlowExample.createCriteria().andReceiveUserIdEqualTo(tUser.getUserId());
                                List<TUserReturnIntegralFlow> tUserReturnIntegralFlows = tUserReturnIntegralFlowMapper.selectByExample(tUserReturnIntegralFlowExample);
                                if(tUserReturnIntegralFlows== null || tUserReturnIntegralFlows.size()==0){
                                    returnIntegralToUser(tOrder, confirmTime, userSession, tUser);
                                }

                            }
                        }
                    }
                    if("1".equals(userLevel)){
                        returnIntegralToUser(tOrder, confirmTime, userSession, tUser);
                    }
                }
            }
//            if("1".equals(userSession.getUserLevel())){//用自己本身是VIP
//                returnIntegralToUser(tOrder,confirmTime,userSession,userSession);
//            }

        }
    }



    private void returnIntegralToUser(TOrder tOrder, String confirmTime, TUser userSession, TUser tUser) {
        List<TDictionary> tDictionaries = queryDictionaryBiz.queryDictionary(Constant.SCALE);
        TDictionary tDictionary = tDictionaries.get(0);
        String value = tDictionary.getDictionaryValue();
        BigDecimal bigDecimal = new BigDecimal(value).multiply(new BigDecimal(tOrder.getOrderPrice()));
        TUserAccountExample tUserAccountExample = new TUserAccountExample();
        tUserAccountExample.createCriteria().andUserIdEqualTo(tUser.getUserId());
        List<TUserAccount> tUserAccounts = tUserAccountMapper.selectByExample(tUserAccountExample);
        if(tUserAccounts!=null && tUserAccounts.size()>0){//返积分
            TUserAccount tUserAccount = tUserAccounts.get(0);
            tUserAccountForUpdateMapper.queryUserAccountForUpdate(tUserAccount.getUserAccountId());
            String userIntegral = tUserAccount.getUserIntegral();

            userIntegral = (bigDecimal.add(new BigDecimal(userIntegral))).toString();
            tUserAccount.setUserIntegral(userIntegral);
            tUserAccountMapper.updateByPrimaryKeySelective(tUserAccount);
            //积分流水
            TUserReturnIntegralFlow tUserReturnIntegralFlow = new TUserReturnIntegralFlow();
            tUserReturnIntegralFlow.setConsumeUserId(userSession.getUserId());
            tUserReturnIntegralFlow.setConsumeUserName(userSession.getUserName());
            tUserReturnIntegralFlow.setCreateTime(confirmTime);
            tUserReturnIntegralFlow.setOrderId(tOrder.getOrderId());
            tUserReturnIntegralFlow.setOrderPrice(tOrder.getOrderPrice());
            tUserReturnIntegralFlow.setReceiveUserId(tUser.getUserId());
            tUserReturnIntegralFlow.setReceiveUserName(tUser.getUserName());
            tUserReturnIntegralFlow.setReturnIntegral(userIntegral);
            tUserReturnIntegralFlow.setUserPointId(SequenceUtil.nextId());
            tUserReturnIntegralFlowMapper.insert(tUserReturnIntegralFlow);
        }
    }
}
